﻿<template>
	<div class="card-fill layout-padding" v-loading="loading" element-loading-text="Loading...">
		<WtmSearcher v-if="searchBar" v-model="searchData" @search="getTableData" :showToggleAdvanced="true" :defaultExpandAdvanced="false">
			<template #primary="{ showAdvanced }">
				<el-row :gutter="15">
					<!-- 常用搜索条件 -->
					<el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb15">
						<el-form-item ref="Name_FormItem" prop="Name">
							<template #label>
								<WtmLabelTooltip label="姓名" tooltip="员工姓名" />
							</template>
							<el-input v-model="searchData.Name" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb15">
						<el-form-item ref="Phone_FormItem" prop="Phone">
							<template #label>
								<WtmLabelTooltip label="手机号" tooltip="手机号码" />
							</template>
							<el-input v-model="searchData.Phone" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb15">
						<el-form-item ref="JobPositionId_FormItem" prop="JobPositionId">
							<template #label>
								<WtmLabelTooltip label="工作岗位" tooltip="关联的工作岗位" />
							</template>
							<el-select v-model="searchData.JobPositionId" :data="stateData.AllSysJobPositions" clearable>
								<el-option v-for="item in stateData.AllSysJobPositions" :key="item.Value" :value="item.Value" :label="item.Text"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb15">
						<el-form-item ref="BillCode_FormItem" prop="BillCode">
							<template #label>
								<WtmLabelTooltip label="单据编号" tooltip="单据编号" />
							</template>
							<el-input v-model="searchData.BillCode" clearable></el-input>
						</el-form-item>
					</el-col>

					<!-- 可展开的详细搜索条件 -->
					<template v-if="showAdvanced">
						<el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb15">
							<el-form-item ref="BillStatus_FormItem" prop="BillStatus">
								<template #label>
									<WtmLabelTooltip label="单据状态" tooltip="单据状态" />
								</template>
								<el-select v-model="searchData.BillStatus" clearable>
									<el-option v-for="status in BILL_STATUS_OPTIONS" :key="status" :value="status" :label="status"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb15">
							<el-form-item ref="WorkflowStatus_FormItem" prop="WorkflowStatus">
								<template #label>
									<WtmLabelTooltip label="工作流状态" tooltip="工作流状态" />
								</template>
								<el-select v-model="searchData.WorkflowStatus" clearable>
									<el-option v-for="status in WORKFLOW_STATUS_OPTIONS" :key="status" :value="status" :label="status"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb15">
							<el-form-item ref="Approver_FormItem" prop="Approver">
								<template #label>
									<WtmLabelTooltip label="审批人" tooltip="流程审批人" />
								</template>
								<el-input v-model="searchData.Approver" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb15">
							<el-form-item ref="ApprovalTime_FormItem" prop="ApprovalTime">
								<template #label>
									<WtmLabelTooltip label="审批时间" tooltip="流程审批时间" />
								</template>
								<el-date-picker type="daterange" v-model="searchData.ApprovalTime" clearable></el-date-picker>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb15">
							<el-form-item ref="EmploymentType_FormItem" prop="EmploymentType">
								<template #label>
									<WtmLabelTooltip label="用工形式" tooltip="用工形式" />
								</template>
								<el-select v-model="searchData.EmploymentType" clearable>
									<el-option v-for="option in EMPLOYMENT_TYPE_OPTIONS" :key="option" :value="option" :label="option"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb15">
							<el-form-item ref="State_FormItem" prop="State">
								<template #label>
									<WtmLabelTooltip label="人员状态" tooltip="人员状态" />
								</template>
								<el-select v-model="searchData.State" clearable>
									<el-option v-for="option in PERSON_STATE_OPTIONS" :key="option" :value="option" :label="option"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
					</template>
				</el-row>
			</template>
		</WtmSearcher>

		<div v-if="toolbar">
			<WtmButton
				v-auth="'/api/SysHrInterviewRequestBill/Add'"
				icon="fa fa-plus"
				type="primary"
				:button-text="$t('message._system.common.vm.add')"
				@click="OnCreateClick()"
			/>
			<WtmButton
				v-auth="'/api/SysHrInterviewRequestBill/BatchDelete'"
				icon="fa fa-trash"
				type="danger"
				:button-text="$t('message._system.common.vm.batchdelete')"
				:disabled="!hasSelectedRows"
				:confirm="$t('message._system.common.vm.deletetip')"
				@click="onBatchDelete()"
			/>
			<WtmButton
				v-auth="'/api/SysHrInterviewRequestBill/Import'"
				icon="fa fa-tasks"
				type="primary"
				:button-text="$t('message._system.common.vm.import')"
				@click="OnImportClick()"
			/>
			<WtmButton
				v-auth="'/api/SysHrInterviewRequestBill/ExportExcel'"
				icon="fa fa-arrow-circle-down"
				type="primary"
				:button-text="$t('message._system.common.vm.export')"
				@click="onExport()"
			/>
		</div>

		<WtmTable ref="tableRef" v-bind="tableData" class="table-operation-area">
			<template #operation>
				<el-table-column :label="$t('message._system.common.vm.operate')" min-width="180" align="center" class-name="operation-column">
					<template v-slot="scope">
						<div class="operation-container">
							<WtmButton
								v-auth="'/api/SysHrInterviewRequestBill/Edit'"
								v-if="scope.row.BillStatus == '草稿'"
								:is-text="true"
								icon="fa fa-pencil-square"
								type="warning"
								:button-text="$t('message._system.common.vm.edit')"
								@click="OnEditRowClick(scope.row)"
							/>
							<WtmButton
								v-auth="'/api/SysHrInterviewRequestBill/{id}'"
								:is-text="true"
								icon="fa fa-info-circle"
								type="info"
								:button-text="$t('message._system.common.vm.detail')"
								@click="OnDetailsRowClick(scope.row)"
							/>
							<WtmButton
								v-auth="'/api/SysHrInterviewRequestBill/BatchDelete'"
								v-if="scope.row.BillStatus == '草稿'"
								icon="fa fa-trash"
								type="danger"
								:button-text="$t('message._system.common.vm.delete')"
								:confirm="$t('message._system.common.vm.deletetip')"
								:is-text="true"
								@click="onDelete(scope.row)"
							/>
						</div>
					</template>
				</el-table-column>
			</template>
		</WtmTable>
	</div>
</template>

<script setup lang="ts" name="应聘申请;true;HengTong.Areas.Hr.Controllers;SysHrInterviewRequestBill">
import { ElMessage } from 'element-plus';
import { defineAsyncComponent, reactive, ref, getCurrentInstance, onMounted, computed, type ComponentInternalInstance } from 'vue';
import { SysHrInterviewRequestBillApi as api } from '/@/views/hr/syshrinterviewrequestbill/api';
import { ISysHrInterviewRequestBillView, ISysHrInterviewRequestBillQuery } from '/@/views/hr/syshrinterviewrequestbill/api/interface';
import other from '/@/utils/other';

// 懒加载子组件
const CreateDialog = defineAsyncComponent(() => import('./create.vue'));
const EditDialog = defineAsyncComponent(() => import('./edit.vue'));
const DetailsDialog = defineAsyncComponent(() => import('./details.vue'));
const ImportDialog = defineAsyncComponent(() => import('./import.vue'));

// 常量定义
const BILL_STATUS_OPTIONS = ['草稿', '正文'] as const;

const WORKFLOW_STATUS_OPTIONS = ['待提交', '提交中', '审核中', '已完成', '已取消'] as const;

const EMPLOYMENT_TYPE_OPTIONS = ['全职', '兼职', '实习', '外包'] as const;

const PERSON_STATE_OPTIONS = ['正常', '停用', '删除'] as const;

const BILL_STATUS = {
	DRAFT: '草稿',
	FORMAL: '正文',
} as const;

const TABLE_HEADERS = [
	// 核心流程信息
	{ title: '单据编号', key: 'BillCode', type: 'text', isCheck: true },
	{
		title: '工作流状态',
		key: 'WorkflowStatus',
		type: 'tag',
		isCheck: true,
		tagType: (row: ISysHrInterviewRequestBillView) => {
			const statusMap: Record<string, string> = {
				待提交: 'info',
				提交中: 'info',
				审核中: 'warning',
				已完成: 'success',
				已取消: 'danger',
			};
			return statusMap[row.WorkflowStatus || ''] || 'info';
		},
	},
	{ title: '单据状态', key: 'BillStatus', type: 'text', isCheck: true },
	{ title: '初试结果', key: 'InitialResult', type: 'text', isCheck: true },
	{ title: '初试评语', key: 'InitialComment', type: 'text', isCheck: true },
	{ title: '复试结果', key: 'ReExamResult', type: 'text', isCheck: true },
	{ title: '复试评语', key: 'ReExamComment', type: 'text', isCheck: true },

	{ title: '姓名', key: 'Name', type: 'text', isCheck: true },
	{ title: '手机号', key: 'Phone', type: 'text', isCheck: true },
	{ title: '最高学历', key: 'HighestEducation', type: 'text', isCheck: true },
	{ title: '专业大类', key: 'MajorCategory', type: 'text', isCheck: true },
	{ title: '专业名称', key: 'MajorName', type: 'text', isCheck: true },
	{ title: '期望薪酬', key: 'ExpectedSalary', type: 'text', isCheck: true },
	{ title: '工作年限(年)', key: 'WorkExperience', type: 'text', isCheck: true },
	{ title: '可到岗时间', key: 'ArrivalDate', type: 'text', isCheck: true },
	{ title: '是否亲属', key: 'HasRelativeInCompany', type: 'text', isCheck: true },
	{ title: '亲属关系', key: 'RelationshipType', type: 'text', isCheck: true },
	{ title: '亲属姓名', key: 'RelativeName', type: 'text', isCheck: true },

	// 基本信息
	{ title: '性别', key: 'Gender', type: 'text', isCheck: true },
	{ title: '出生日期', key: 'BirthDate', type: 'text', isCheck: true },
	{ title: '生育状况', key: 'FertilityStatus', type: 'text', isCheck: true },
	{ title: '身份证号', key: 'IdCardNo', type: 'text', isCheck: true },
	{ title: '邮箱', key: 'Email', type: 'text', isCheck: true },
	{ title: '家庭住址', key: 'HomeAddress', type: 'text', isCheck: true },
	{ title: '原薪酬', key: 'CurrentSalary', type: 'text', isCheck: true },
	{ title: '备注', key: 'Remark', type: 'text', isCheck: true },


	// 详细面试信息
	{ title: '初试时间', key: 'InitialTestTime', type: 'text', isCheck: true },

	{ title: '复试时间', key: 'ReExamTime', type: 'text', isCheck: true },
	{ title: '最终结果', key: 'FinalResult', type: 'text', isCheck: true },
	{ title: '最终结果说明', key: 'FinalRemark', type: 'text', isCheck: true },
	{ title: '拒绝原因', key: 'RejectReason', type: 'text', isCheck: true },

	// 照片信息
	{ title: '照片', key: 'PhotoId', type: 'image', isCheck: true },
	{ title: '身份证人像面', key: 'IdCardFrontPhotoId', type: 'image', isCheck: true },
	{ title: '身份证国徽面', key: 'IdCardBackPhotoId', type: 'image', isCheck: true },

	// 人员基本信息
	{ title: '工号', key: 'StaffNo', type: 'text', isCheck: true },
	{ title: '年龄', key: 'Age', type: 'text', isCheck: true },
	{ title: '身高(cm)', key: 'Height', type: 'text', isCheck: true },
	{ title: '体重(kg)', key: 'Weight', type: 'text', isCheck: true },
	{ title: '婚姻状况', key: 'MaritalStatus', type: 'text', isCheck: true },
	{ title: '户口所在地', key: 'HouseholdRegistration', type: 'text', isCheck: true },
	{ title: '籍贯', key: 'NativePlace', type: 'text', isCheck: true },
	{ title: '政治面貌', key: 'PoliticalStatus', type: 'text', isCheck: true },
	{ title: '入党(团)日期', key: 'PartyJoinDate', type: 'text', isCheck: true },
	{ title: '入党所在支部', key: 'PartyBranch', type: 'text', isCheck: true },
	{ title: '血型', key: 'BloodType', type: 'text', isCheck: true },
	{ title: '兴趣爱好特长', key: 'Hobbies', type: 'text', isCheck: true },

	// 档案信息
	{ title: '档案所在地', key: 'ArchiveLocation', type: 'text', isCheck: true },
	{ title: '存档号', key: 'ArchiveNo', type: 'text', isCheck: true },
	{ title: '档案调出日期', key: 'ArchiveTransferOutDate', type: 'text', isCheck: true },
	{ title: '档案调入日期', key: 'ArchiveTransferInDate', type: 'text', isCheck: true },

	// 薪资信息
	{ title: '工资卡银行', key: 'SalaryBank', type: 'text', isCheck: true },
	{ title: '工资卡号', key: 'SalaryAccount', type: 'text', isCheck: true },
	{ title: '奖金卡银行', key: 'BonusBank', type: 'text', isCheck: true },
	{ title: '奖金卡号', key: 'BonusAccount', type: 'text', isCheck: true },

	// 组织信息
	{ title: '人事组织', key: 'OrgName', type: 'text', isCheck: true },
	{ title: '所属部门', key: 'GroupName', type: 'text', isCheck: true },
	{ title: '工作岗位', key: 'JobPositionName', type: 'text', isCheck: true },

	// 简历信息
	{ title: '简历', key: 'ResumeId', type: 'text', isCheck: true },

	// 入职信息
	{ title: '入职日期', key: 'HireDate', type: 'text', isCheck: true },
	{ title: '转正日期', key: 'ConfirmationDate', type: 'text', isCheck: true },
	{ title: '用工形式', key: 'EmploymentType', type: 'text', isCheck: true },

	// 合同信息
	{ title: '劳动合同类型', key: 'ContractType', type: 'text', isCheck: true },
	{ title: '合同编号', key: 'ContractNo', type: 'text', isCheck: true },
	{ title: '合同起始日', key: 'ContractStartDate', type: 'text', isCheck: true },
	{ title: '合同终止日', key: 'ContractEndDate', type: 'text', isCheck: true },

	// 离职信息
	// { title: '离职日期', key: 'ResignationDate', type: 'text', isCheck: true },
	// { title: '离职原因', key: 'ResignationReason', type: 'text', isCheck: true },
	// { title: '离职类型', key: 'ResignationType', type: 'text', isCheck: true },
	// { title: '人员状态', key: 'State', type: 'text', isCheck: true },

	// { title: '学习经历', key: 'PersonEducationsName', type: 'text', isCheck: false },
	// { title: '家庭成员', key: 'PersonFamiliesName', type: 'text', isCheck: false },
	// { title: '工作经历', key: 'PersonWorksName', type: 'text', isCheck: false },
	// { title: '培训考试', key: 'PersonTrainExamsName', type: 'text', isCheck: false },
	// { title: '员工体检', key: 'PersonMedicalsName', type: 'text', isCheck: false },
	// { title: '员工履历', key: 'PersonResumesName', type: 'text', isCheck: false },
	// { title: '员工合同', key: 'PersonContractsName', type: 'text', isCheck: false },
] as const;

const ci = getCurrentInstance() as ComponentInternalInstance;
const loading = ref(false);

const props = withDefaults(
	defineProps<{
		/** 是否显示搜索(筛选)栏 */
		searchBar?: boolean;
		/** 是否显示工具栏 */
		toolbar?: boolean;
		/** 是否禁用表格任何操作 */
		isDisabled?: boolean;
		/** 默认筛选 */
		filters?: ISysHrInterviewRequestBillQuery;
		/** 主表ID */
		entityId?: string | null;
	}>(),
	{
		searchBar: true,
		toolbar: true,
		isDisabled: true,
		filters: () => ({}),
		entityId: null,
	}
);

const stateData = reactive({});

const searchData = ref<ISysHrInterviewRequestBillQuery>({});
const tableRef = ref();

// 初始化表格数据
const tableData = ref({
	data: [],
	header: TABLE_HEADERS,
	config: {
		total: 0,
		loading: true,
		isSerialNo: true,
		isSelection: true,
		isOperate: props.isDisabled,
	},
});

// 计算属性 - 判断是否有选中行
const hasSelectedRows = computed(() => {
	const selectedRows = tableRef.value?.getSelectedRows() || [];
	return selectedRows.length > 0;
});

// 统一的错误处理函数
const handleError = (error: any) => {
	other.setFormError(ci, error);
	loading.value = false;
};

// 初始化表格数据
const getTableData = async () => {
	try {
		await tableRef.value.doSearch(api().search, searchData.value);
	} catch (error) {
		handleError(error);
	}
};

// 对话框操作
const OnCreateClick = () => {
	other.openDialog(ci.proxy?.$t('message._system.common.vm.add') || '新增', CreateDialog, null, getTableData);
};

const OnEditRowClick = (row: ISysHrInterviewRequestBillView) => {
	other.openDialog(ci.proxy?.$t('message._system.common.vm.edit') || '编辑', EditDialog, row, getTableData);
};

const OnDetailsRowClick = (row: ISysHrInterviewRequestBillView) => {
	other.openDialog(ci.proxy?.$t('message._system.common.vm.detail') || '详情', DetailsDialog, row, getTableData);
};

const OnImportClick = () => {
	other.openDialog(ci.proxy?.$t('message._system.common.vm.import') || '导入', ImportDialog, null, getTableData);
};

// 删除操作
const onDelete = async (row: ISysHrInterviewRequestBillView) => {
	try {
		const ids = [row.ID] as (string | number)[];
		await api().delete(ids);
		getTableData();
	} catch (error) {
		handleError(error);
	}
};

// 批量删除
const onBatchDelete = async () => {
	const selectedRows = tableRef.value?.getSelectedRows();
	const selectedIds = selectedRows?.map((x: ISysHrInterviewRequestBillView) => x.ID) as (string | number)[];

	if (selectedIds && selectedIds.length > 0) {
		try {
			await api().delete(selectedIds);
			tableRef.value?.clearSelection();
			getTableData();
		} catch (error) {
			handleError(error);
		}
	} else {
		ElMessage.error(ci.proxy?.$t('message._system.common.vm.check') || '请选择要删除的数据');
	}
};

// 导出操作
const onExport = () => {
	const selected = tableRef.value?.getSelectedRows();
	if (selected && selected.length > 0) {
		api().exportById(selected.map((x: any) => x.ID));
	} else {
		api().export({});
	}
};

// 加载选择列表数据
const loadSelectListData = async () => {
	// 列表页面不需要加载选择列表数据
};

// 页面加载时
onMounted(async () => {
	loading.value = true;

	try {
		// 并行加载选择列表数据和表格数据
		await Promise.all([loadSelectListData(), getTableData()]);
	} catch (error) {
		handleError(error);
	} finally {
		loading.value = false;
	}
});
</script>
